import cv2
from matplotlib import pyplot as plt
import matplotlib
import baseconf

matplotlib.use('TkAgg')

fristvideo_path = baseconf.BASE_DISK+":/datasets_path/tianchivit/fristvideo"

cap = cv2.VideoCapture(fristvideo_path+'./43.avi')
img1 = cap.read()[1]
for _ in range(30):
    cap.read()
img2 = cap.read()[1]


img1=cv2.cvtColor(img1,  cv2.COLOR_BGR2RGB)
img2=cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

sift = cv2.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)


# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2,k=2)
# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.35*n.distance:
        good.append([m])
# cv.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

plt.figure(figsize=(10, 5))
plt.imshow(img3),plt.show()